<template>
  <n-config-provider :theme="theme" :theme-overrides="themeOverrides">
    <div class="reset-container">
      <div class="theme-switch" @click="toggleTheme">
        <n-icon size="22">
          <SunnyOutline v-if="theme === null" />
          <MoonOutline v-else />
        </n-icon>
      </div>
      
      <div class="reset-box">
        <div class="reset-left">
          <div class="welcome-text">
            <h1>重置密码</h1>
            <p>重新设置您的密码</p>
          </div>
        </div>
        
        <n-card class="reset-right" :bordered="false">
          <div class="reset-header">
            <h2>重置密码</h2>
            <p class="subtitle">请填写以下信息</p>
          </div>
          
          <n-form
            ref="formRef"
            :model="formValue"
            :rules="rules"
            label-placement="left"
            label-width="0"
            size="large"
          >
            <n-form-item path="username">
              <n-input
                v-model:value="formValue.username"
                placeholder="请输入用户名"
                clearable
              >
                <template #prefix>
                  <n-icon :component="PersonOutline" :color="theme === null ? '#64748B' : '#94A3B8'" />
                </template>
              </n-input>
            </n-form-item>

            <n-form-item path="oldPassword">
              <n-input
                v-model:value="formValue.oldPassword"
                type="password"
                show-password-on="click"
                placeholder="请输入原密码"
              >
                <template #prefix>
                  <n-icon :component="LockClosedOutline" :color="theme === null ? '#64748B' : '#94A3B8'" />
                </template>
              </n-input>
            </n-form-item>

            <n-form-item path="newPassword">
              <n-input
                v-model:value="formValue.newPassword"
                type="password"
                show-password-on="click"
                placeholder="请输入新密码"
              >
                <template #prefix>
                  <n-icon :component="LockClosedOutline" :color="theme === null ? '#64748B' : '#94A3B8'" />
                </template>
              </n-input>
            </n-form-item>

            <n-form-item path="confirmPassword">
              <n-input
                v-model:value="formValue.confirmPassword"
                type="password"
                show-password-on="click"
                placeholder="请确认新密码"
              >
                <template #prefix>
                  <n-icon :component="LockClosedOutline" :color="theme === null ? '#64748B' : '#94A3B8'" />
                </template>
              </n-input>
            </n-form-item>
          </n-form>

          <div style="margin-top: 24px">
            <n-button
              type="primary"
              size="large"
              block
              :loading="loading"
              @click="handleReset"
              class="submit-btn"
            >
              重置密码
            </n-button>
          </div>

          <div class="tips" style="margin-top: 16px; text-align: center;">
            <n-button text @click="router.push('/login')">
              返回登录
            </n-button>
          </div>

          <div class="reset-footer">
            <n-text depth="3">
              Copyright © {{ new Date().getFullYear() }}
            </n-text>
          </div>
        </n-card>
      </div>
    </div>
  </n-config-provider>
</template>

<script setup>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useMessage } from 'naive-ui'
import { PersonOutline, LockClosedOutline, SunnyOutline, MoonOutline } from '@vicons/ionicons5'
import { updatePassword } from '@/api/auth'
import { useThemeStore } from '@/stores/theme'

const router = useRouter()
const message = useMessage()
const formRef = ref(null)
const loading = ref(false)

const themeStore = useThemeStore()
const theme = computed(() => themeStore.theme)
const themeOverrides = computed(() => themeStore.themeOverrides)
const toggleTheme = () => themeStore.toggleTheme()

const formValue = reactive({
  username: '',
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

const validatePassword = (rule, value) => {
  if (!value) {
    return new Error('请输入密码')
  }
  if (value.length < 6) {
    return new Error('密码不能少于6个字符')
  }
  return true
}

const validateConfirmPassword = (rule, value) => {
  if (!value) {
    return new Error('请确认新密码')
  }
  if (value !== formValue.newPassword) {
    return new Error('两次输入密码不一致')
  }
  return true
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  oldPassword: [
    { required: true, message: '请输入原密码', trigger: 'blur' }
  ],
  newPassword: [
    { validator: validatePassword, trigger: 'blur' }
  ],
  confirmPassword: [
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
}

const handleReset = () => {
  if (!formRef.value) return
  
  formRef.value.validate(async (errors) => {
    if (!errors) {
      loading.value = true
      try {
        const { username, oldPassword, newPassword, confirmPassword } = formValue
        await updatePassword({ username, oldPassword, newPassword, confirmPassword })
        message.success('密码重置成功')
        router.push('/login')
      } catch (error) {
        console.error('重置失败:', error)
        message.error(error.message || '重置失败，请重试')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.reset-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: v-bind('theme === null ? "linear-gradient(135deg, #F0F9FF 0%, #E0F2FE 100%)" : "linear-gradient(135deg, #17171A 0%, #1C1C1C 100%)"');
  position: relative;
  overflow: hidden;
}

.reset-box {
  display: flex;
  width: 900px;
  height: 600px;
  border-radius: 24px;
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.1);
}

.reset-left {
  flex: 1;
  background: v-bind('theme === null ? "linear-gradient(135deg, #0EA5E9 0%, #0284C7 100%)" : "linear-gradient(135deg, #101010 0%, #1A1A1A 100%)"');
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
  position: relative;
  overflow: hidden;
}

.welcome-text {
  color: #ffffff;
  text-align: center;
  position: relative;
  z-index: 1;
}

.welcome-text h1 {
  font-size: 32px;
  margin-bottom: 12px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.welcome-text p {
  font-size: 16px;
  opacity: 0.9;
  letter-spacing: 0;
}

.reset-right {
  flex: 1;
  padding: 24px 40px;
  display: flex;
  flex-direction: column;
  position: relative;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

.reset-header {
  text-align: center;
  margin-bottom: 20px;
}

.reset-header h2 {
  font-size: 24px;
  margin-bottom: 4px;
  font-weight: 600;
  color: v-bind('theme === null ? "#0369A1" : "#C2C7D0"');
  letter-spacing: 0.5px;
}

.subtitle {
  font-size: 14px;
  letter-spacing: 0;
}

:deep(.n-form-item) {
  margin-bottom: 12px;
}

:deep(.n-form-item-feedback-wrapper) {
  min-height: 18px;
}

:deep(.n-input) {
  height: 36px;
  line-height: 36px;
}

:deep(.n-input__input-el) {
  height: 36px;
  line-height: 36px;
  padding-top: 0;
  padding-bottom: 0;
}

:deep(.n-input__prefix) {
  height: 36px;
  margin-right: 8px;
  display: flex;
  align-items: center;
}

:deep(.n-input-wrapper) {
  padding-top: 0;
  padding-bottom: 0;
}

.tips {
  margin-top: 12px;
  text-align: center;
}

.reset-footer {
  margin-top: auto;
  text-align: center;
  padding: 12px 0;
}

.theme-switch {
  position: absolute;
  top: 20px;
  right: 20px;
  cursor: pointer;
  background: v-bind('theme === null ? "rgba(0, 0, 0, 0.05)" : "rgba(255, 255, 255, 0.1)"');
  border-radius: 50%;
  padding: 10px;
  backdrop-filter: blur(8px);
  transition: all 0.3s ease;
  color: v-bind('theme === null ? "#64748B" : "#94A3B8"');
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.theme-switch:hover {
  background: v-bind('theme === null ? "rgba(0, 0, 0, 0.1)" : "rgba(255, 255, 255, 0.15)"');
  color: v-bind('theme === null ? "#0EA5E9" : "#E2E8F0"');
  transform: rotate(30deg);
}
</style> 